package com.sihai.ehr.web.action.train;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.TrainCategory;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.TrainCategoryService;
import com.sihai.ehr.web.action.EHRAction;

/**
 * 培训类型控制器
 * 
 * @author henry
 * 
 */
@Results({ @Result(name = "main", location = "train_main.jsp"),
		@Result(name = "category_manage", location = "category_manage.jsp"),
		@Result(name = "list", location = "category_list.jsp") })
public class TrainCategoryAction extends EHRAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Log log = LogFactory.getLog(TrainCategoryAction.class);

	@BeanInject
	private OrganizationService organizationService;
	@BeanInject
	private TrainCategoryService trainCategoryService;
	private List<Organization> organizations;
	private LimitedList<TrainCategory> categories;
	private TrainCategory category;
	private Long categoryId; // 分类ID
	private String ids;

	@Action("/train/category_main")
	@Override
	public String execute() throws Exception {
		listTitle = "培训类别管理";
		listAction = "/train/category_list.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = false;
		return "main";
	}

	@Action("/train/category_list")
	public String list() {
		listAction = "/train/category_list.xhtml";
		categories = trainCategoryService.getTrainCategories(organizationId,
				createLimit(), createSorter());
		paging(categories);
		/* 设置翻页action */
		this.setPageUrl("train/category_list.xhtml");
		return "list";
	}

	/**
	 * 分类管理处理
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/train/category_manage")
	public String dispatchCategory() throws Exception {
		if (categoryId == null || categoryId <= 0) {
			// 添加
			category = new TrainCategory();
			category.setOrganization(organizationService.get(organizationId));
		} else {
			// 修改
			category = trainCategoryService.getTrainCategory(categoryId);
		}
		return "category_manage";
	}

	/**
	 * 提交培训类别
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/train/post_category")
	public String postCategory() throws Exception {
		//
		if (category == null) {
			if (log.isErrorEnabled()) {
				log.error("提交数据为空");
				return ERROR;
			}
		}
		if (category.getId() == null) {
			// 添加
		} else {
			// 修改
		}

		// 保存数据
		try {
			trainCategoryService.save(category);
		} catch (Exception e) {
			log.equals(e.getMessage());
			return ERROR;
		}
		return "main";
	}

	/**
	 * 删除培训类别
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/train/category_delete", results = { @Result(name = "success", type = "plainText") })
	public String deleteCategory() throws Exception {
		if (StringUtils.isNotBlank(ids)) {
			try {
				trainCategoryService.deleteTrainCategory(ids);
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(e.getMessage());
				}
				return ERROR;
			}

		}

		return SUCCESS;
	}

	public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}

	public List<Organization> getOrganizations() {
		return organizations;
	}

	public void setCategories(LimitedList<TrainCategory> categories) {
		this.categories = categories;
	}

	public LimitedList<TrainCategory> getCategories() {
		return categories;
	}

	public TrainCategory getCategory() {
		return category;
	}

	public void setCategory(TrainCategory category) {
		this.category = category;
	}

	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	public String getIds() {
		return ids;
	}
	
	public void setIds(String ids) {
		this.ids = ids;
	}
}
